<?php

namespace app\api\controller\wanlshop;

use addons\wanlshop\library\WanlPay\WanlPay;
use app\common\controller\Api;
use think\Db;
use think\Env;

/**
 * WanlShop支付接口
 */
class Pay extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 获取支付信息
     *
     * @ApiSummary  (WanlShop 获取支付信息)
     * @ApiMethod   (POST)
     *
     * @param string $id 订单ID
     */
    public function getPay()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $id = $this->request->post('order_id');
            $id ? $id : ($this->error(__('非法请求')));
            // 判断权限
            $orderState = model('app\api\model\wanlshop\Order')
                ->where(['id' => $id, 'user_id' => $this->auth->id])
                ->find();
            $orderState['state'] != 1 ? ($this->error(__('订单异常'))) : '';
            // 获取支付信息
            $pay = model('app\api\model\wanlshop\Pay')
                ->where('order_id', $id)
                ->field('id,order_id,order_no,pay_no,price')
                ->find();
            $this->success('ok', $pay);
        }
        $this->error(__('非法请求'));
    }

    /**
     * 支付订单
     *
     * @ApiSummary  (WanlShop 支付订单)
     * @ApiMethod   (POST)
     *
     * @param string $order_id 订单ID
     * @param string $type 支付类型
     */
    public function payment()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $order_id = $this->request->post('order_id/a');
            $order_id ? $order_id : ($this->error(__('非法请求')));
            $type = $this->request->post('type');
            $method = $this->request->post('method');
            $code = $this->request->post('code');
            $user_id = $this->auth->id;
            $type ? $type : ($this->error(__('未选择支付类型')));
            // 判断权限
            $order = model('app\api\model\wanlshop\Order')
                ->where('id', 'in', $order_id)
                ->where('user_id', $user_id)
                ->select();
            if (!$order) {
                $this->error(__('没有找到任何要支付的订单'));
            }
            foreach ($order as $item) {
                if ($item['state'] != 1) {
                    $this->error(__('订单已支付，或网络繁忙'));
                }
                // 1.0.5升级 修复付款减库存
                foreach (model('app\api\model\wanlshop\OrderGoods')->where('order_id', $item['id'])->select() as $data) {
                    // 获取sku
                    $sku = model('app\api\model\wanlshop\GoodsSku')->get($data['goods_sku_id']);
                    // 检查库存
                    if ($sku['stock'] < $data['number']) $this->error(__('无法购买，库存不足'));
                    // 查询商品
                    $goods = model('app\api\model\wanlshop\Goods')
                        ->where(['id' => $data['goods_id'], 'stock' => 'payment'])
                        ->find();
                    // 库存计算方式:porder=下单减库存,payment=付款减库存
                    if ($goods) $sku->setDec('stock', $data['number']);
                }
            }
            // 调用支付
            $wanlPay = new WanlPay($type, $method, $code);
            $data = $wanlPay->pay($order_id);
            if ($data['code'] == 200) {
                $this->success('ok', $data['data']);
            } else {
                $this->error($data['msg']);
            }
        }
        $this->error(__('非正常请求'));
    }

    /**
     * 用户充值
     */
    public function recharge()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $money = $this->request->post('money');
            $type = $this->request->post('type');
            $method = $this->request->post('method');
            $code = $this->request->post('code');
            $user_id = $this->auth->id;
            $type ? $type : ($this->error(__('未选择支付类型')));
            $money ? $money : ($this->error(__('为输入充值金额')));
            // 调用支付
            $wanlPay = new WanlPay($type, $method, $code);
            $data = $wanlPay->recharge($money);
            if ($data['code'] == 200) {
                $this->success($data['msg'], $data['data']);
            } else {
                $this->error($data['msg']);
            }
        }
        $this->error(__('非正常请求'));
    }

    /**
     * 用户提现账户
     */
    public function getPayAccount()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $row = model('app\api\model\wanlshop\PayAccount')
                ->where(['user_id' => $this->auth->id])
                ->order('createtime desc')
                ->select();
            $this->success('ok', $row);
        }
        $this->error(__('非正常请求'));
    }

    /**
     * 新增提现账户
     */
    public function addPayAccount()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['user_id'] = $this->auth->id;
            $row = model('app\api\model\wanlshop\PayAccount')->allowField(true)->save($post);
            if ($row) {
                $this->success('ok', $row);
            } else {
                $this->error(__('新增失败'));
            }
        }
        $this->error(__('非正常请求'));
    }

    /**
     * 删除提现账户
     */
    public function delPayAccount($ids = '')
    {
        $row = model('app\api\model\wanlshop\PayAccount')
            ->where('id', 'in', $ids)
            ->where(['user_id' => $this->auth->id])
            ->delete();
        if ($row) {
            $this->success('ok', $row);
        } else {
            $this->error(__('删除失败'));
        }
    }

    /**
     * 初始化提现
     */
    public function initialWithdraw()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $config = get_addon_config('wanlshop');
            $bank = model('app\api\model\wanlshop\PayAccount')
                ->where(['user_id' => $this->auth->id])
                ->order('createtime desc')
                ->find();
            $this->success('ok', [
                'money' => $this->auth->money,
                'servicefee' => $config['withdraw']['servicefee'],
                'bank' => $bank
            ]);
        }
        $this->error(__('非正常请求'));
    }

    /**
     * 用户提现
     */
    public function withdraw()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            // 金额
            $money = $this->request->post('money');
            // 账户
            $account_id = $this->request->post('account_id');
            if ($money <= 0) {
                $this->error('提现金额不正确');
            }
            if ($money > $this->auth->money) {
                $this->error('提现金额超出可提现额度');
            }
            if (!$account_id) {
                $this->error("提现账户不能为空");
            }
            // 查询提现账户
            $account = \app\api\model\wanlshop\PayAccount::where(['id' => $account_id, 'user_id' => $this->auth->id])->find();
            if (!$account) {
                $this->error("提现账户不存在");
            }
            $config = get_addon_config('wanlshop');
            if ($config['withdraw']['state'] == 'N') {
                $this->error("系统该关闭提现功能，请联系平台客服");
            }
            if (isset($config['withdraw']['minmoney']) && $money < $config['withdraw']['minmoney']) {
                $this->error('提现金额不能低于' . $config['withdraw']['minmoney'] . '元');
            }
            if ($config['withdraw']['monthlimit']) {
                $count = \app\api\model\wanlshop\Withdraw::where('user_id', $this->auth->id)->whereTime('createtime', 'month')->count();
                if ($count >= $config['withdraw']['monthlimit']) {
                    $this->error("已达到本月最大可提现次数");
                }
            }
            // 计算提现手续费
            if ($config['withdraw']['servicefee'] && $config['withdraw']['servicefee'] > 0) {
                $servicefee = number_format($money * $config['withdraw']['servicefee'] / 1000, 2);
                $handingmoney = $money - number_format($money * $config['withdraw']['servicefee'] / 1000, 2);
            } else {
                $servicefee = 0;
                $handingmoney = $money;
            }
            Db::startTrans();
            try {
                $data = [
                    'user_id' => $this->auth->id,
                    'money' => $handingmoney,
                    'handingfee' => $servicefee, // 手续费
                    'type' => $account['bankCode'],
                    'account' => $account['cardCode'],
                    'orderid' => date("Ymdhis") . sprintf("%08d", $this->auth->id) . mt_rand(1000, 9999)
                ];
                $withdraw = \app\api\model\wanlshop\Withdraw::create($data);
                $pay = new WanlPay;
                $pay->money(-$money, $this->auth->id, '申请提现', 'withdraw', $withdraw['id']);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('提现申请成功！请等待后台审核', $this->auth->money);
        }
        $this->error(__('非正常请求'));
    }

    /**
     * 获取支付日志
     */
    public function withdrawLog()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $list = model('app\api\model\wanlshop\Withdraw')
                ->where('user_id', $this->auth->id)
                ->order('createtime desc')
                ->paginate();
            $this->success('ok', $list);
        }
        $this->error(__('非法请求'));
    }

    /**
     * 获取支付日志
     */
    public function moneyLog()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $list = model('app\common\model\MoneyLog')
                ->where('user_id', $this->auth->id)
                ->order('createtime desc')
                ->paginate();
            $this->success('ok', $list);
        }
        $this->error(__('非法请求'));
    }

    /**
     * 获取支付详情
     */
    public function details($id = null, $type = null)
    {
        if ($type == 'pay') {
            $field = 'id,shop_id,createtime,paymenttime';
            $order = model('app\api\model\wanlshop\Order')
                ->where('order_no', 'in', $id)
                ->where('user_id', $this->auth->id)
                ->field($field)
                ->select();
            //1.0.5升级 临时修改,后续升级版本重构
            if (!$order) {
                $shop = model('app\api\model\wanlshop\Shop')->get(['user_id' => $this->auth->id]);
                $order = model('app\api\model\wanlshop\Order')
                    ->where('order_no', 'in', $id)
                    ->where('shop_id', $shop['id'])
                    ->field($field)
                    ->select();
                if (!$order) $this->error(__('订单异常'));
            }
            foreach ($order as $vo) {
                $vo->pay->visible(['price', 'pay_no', 'order_no', 'order_price', 'trade_no', 'actual_payment', 'freight_price', 'discount_price', 'total_amount']);
                $vo->shop->visible(['shopname']);
                $vo->goods = model('app\api\model\wanlshop\OrderGoods')
                    ->where(['order_id' => $vo['id']])
                    ->field('id,title,difference,image,price,number')
                    ->select();
            }
            $this->success('ok', $order);
        } else if ($type == 'recharge' || $type == 'withdraw') { // 用户充值
            if ($type == 'recharge') {
                $model = model('app\api\model\wanlshop\RechargeOrder');
                $field = 'id,paytype,orderid,memo';
            } else {
                $model = model('app\api\model\wanlshop\Withdraw');
                $field = 'id,money,handingfee,status,type,account,orderid,memo,transfertime';
            }
            $row = $model
                ->where(['id' => $id, 'user_id' => $this->auth->id])
                ->field($field)
                ->find();
            $this->success('ok', $row);
        } else if ($type == 'refund') {
            $order = model('app\api\model\wanlshop\Order')
                ->where('order_no', $id)
                ->where('user_id', $this->auth->id)
                ->field('id,shop_id,order_no,createtime,paymenttime')
                ->find();
            if (!$order) {
                $this->error(__('订单异常'));
            }
            $order->shop->visible(['shopname']);
            $order['refund'] = model('app\api\model\wanlshop\Refund')
                ->where(['order_id' => $order['id'], 'user_id' => $this->auth->id])
                ->field('id,price,type,reason,createtime,completetime')
                ->find();
            $this->success('ok', $order);
        } else { // 系统
            $this->success('ok');
        }
    }

    /**
     * 获取余额
     */
    public function getBalance()
    {
        $this->success('ok', $this->auth->money);
    }


    /**
     * 365会员支付
     *
     * @author G
     * @date 2021-08-25 下午8:51
     */
    public function open_vip()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {

            // 会员费
            $money = Env::get('alading.vip_price', 365);

            $type = $this->request->post('type', 'wechat');
            $method = $this->request->post('method');
            $code = $this->request->post('code');
            $user_id = $this->auth->id;

            // 邀请人手机号
            $invite_mobile = $this->request->post('invite_mobile');

            // 获取用户信息
            $user_info = $this->auth->getUser();

            if ($invite_mobile == $user_info['mobile']) {
                $this->error('邀请人手机号不能为本人手机号');
            }

            // 用户 代理级别验证
            if ($user_info['group_id'] == 2) {
                $this->error('您已经是365会员，无需重复购买');
            }else if ($user_info['group_id'] == 3) {
                $this->error('您是区级代理用户，无需购买365会员');
            }else if ($user_info['group_id'] == 4) {
                $this->error('您是市级代理用户，无需购买365会员');
            }

            if (!$user_info['referrer_uid']) {
                // 更新用户邀请人
                if ($invite_mobile) {
                    $invite_user = \app\admin\model\User::where('mobile', $invite_mobile)->find();
                    if (!$invite_user) {
                        $this->error('无邀请人手机号');
                    }
                    \app\admin\model\User::where(['id' => $user_id])->update(['referrer_uid' => $invite_user['id']]);
                }
            }else {
                $invite_user = \app\admin\model\User::where('id', $user_info['referrer_uid'])->find();
            }

            // 详细地址
            $address = $this->request->post('address');
            if ($address) {
                \app\admin\model\User::where(['id' => $user_id])->update(['address' => $address]);
            }

            // 调用支付
            $wanlPay = new WanlPay($type, $method, $code);

            $data = $wanlPay->open_vip($money, $user_info, $invite_user);

            if ($data['code'] == 200) {
                $this->success($data['msg'], $data['data']);
            } else {
                $this->error($data['msg']);
            }
        }
        $this->error(__('非正常请求'));
    }

}